Date: Janurary 27 2017
Click each marker to show cooresponding zip code and city & state names.
library(leaflet)
library(zipcode)
library(maps)
data("zipcode") # load data zipcode
str(zipcode) # dataset contain city state, latitude, and longitude of each zipcode
## 'data.frame': 44336 obs. of 5 variables:
## $ zip : chr "00210" "00211" "00212" "00213" ...
## $ city : chr "Portsmouth" "Portsmouth" "Portsmouth" "Portsmouth" ...
## $ state : chr "NH" "NH" "NH" "NH" ...
## $ latitude : num 43 43 43 43 43 ...
## $ longitude: num -71 -71 -71 -71 -71 ...
CleanZ<-zipcode[!(is.na(zipcode[,4])|is.na(zipcode[,5])),] # remove rows with missing latitude and longitude
mapStates <- map("county", fill = TRUE, plot = FALSE) # mapdata of US county boundaries
Zip_map<-leaflet(data = mapStates) %>% # creates a Leaflet map based on the county boundries
addTiles() %>% # Add graphics elements and layers to the map widget
addPolygons(fillColor = topo.colors(10, alpha = NULL), stroke = FALSE)%>% # fill counties with colors
addMarkers(lat=CleanZ$latitude,lng=CleanZ$longitude,
popup = paste0("Zip Code: ",CleanZ$zip,'<br/>', CleanZ$city, ", ", CleanZ$state) ,
clusterOptions = markerClusterOptions()) # add zipcode latitude, longtitude, and city & state names
Zip_map #display the Zip Code Leaflet map